from flask import Flask,render_template,request,escape
from API import tianqi
from log_request import log_request
import pandas as pd
app=Flask(__name__)

@app.route('/')
def hello() -> "html":
	return "欢迎进入我的网站~"

@app.route('/entry',methods=['GET'])
def hello_entry() -> "html":
	return render_template('entry.html',the_title="欢迎进入天气查询~")

@app.route('/weather', methods=['GET','POST'])
def hello_weather() -> 'html':
	key = '5bd1ca4e7bc885ff80882a4f8b45093b'
	city = request.form['city']
	title = '查询结果如下：'
	res = tianqi(key, city)

	result_humidity = res['humidity']
	result_winddirection = res['winddirection']
	result_windpower =res['windpower']
	result_temperature = res['temperature']
	result_weather = res['weather']
	result_reporttime =res['reporttime']

	log_request(request,res)
	return render_template("jieguo.html",
						   city=city,
						   the_title=title,
						   result_humidity=result_humidity,
						   result_winddirection=result_winddirection,
						   result_windpower=result_windpower,
						   result_temperature =result_temperature,
						   result_weather=result_weather,
						   result_reporttime =result_reporttime)

#日志系统
@app.route('/viewlog',methods=['POST','GET'])
def view_the_log() -> 'html':
    contents = []
    with open('vsearch.log','r') as log:
        for line in log:
            contents.append([])
            for item in line.split('|'):
                contents[-1].append(escape(item))
    len(contents)
    titles = ('Form Data','Remote_addr','User_agent','Results')
    return render_template('viewlog.html',
                           the_title='View Log',
                           row_title=titles,
                           the_data=contents)


if __name__=='__main__':
    app.run(debug=True)